﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Microhard.Framework.MVCHelper
{
    public class ScriptGridView : BaseButton
    {
        private string _onRowSelectedFuncName = null;
        private string _controllerName = null;
        private string _actionName = null;

        public ScriptGridView(string onRowSelectedFuncName)
        {
            this._assetKey = SysConst.OPERATION_VIEW;
            this._onRowSelectedFuncName = onRowSelectedFuncName;
        }

        public override System.Web.Mvc.MvcHtmlString WriteHtml(System.Web.Mvc.HtmlHelper helper, string linkText, string actionName, string controllerName)
        {
            this._controllerName = controllerName;
            this._actionName = actionName;

            return new System.Web.Mvc.MvcHtmlString(string.Empty);
        }


        public override string WriteJQueryReadyOutterScript()
        {
            StringBuilder onSelected = new StringBuilder();
            onSelected.Append("function " + this._onRowSelectedFuncName + "(e) {");
            onSelected.Append("var theGrid = $('#us-grid').data('tGrid');");
            onSelected.Append("dataID = e.row.cells[0].innerHTML;");
            onSelected.Append("var viewHtml = '';");
            onSelected.Append("var parms = 'identity=' + dataID;");
            onSelected.Append("$.ajax({");
            onSelected.Append("url: '/" + this._controllerName + "/" + this._actionName + "',");
            onSelected.Append("type: 'POST',");
            onSelected.Append("data: parms,");
            onSelected.Append("success: function (data) {");
            onSelected.Append("var window = $('#ViewDetail').data('tWindow');");
            onSelected.Append("window.content(data);");
            onSelected.Append("window.center();");
            onSelected.Append("window.open();");
            onSelected.Append("return;");
            onSelected.Append(" }");
            onSelected.Append("});");
            onSelected.Append(" }");
            return onSelected.ToString();
        }
    }
}
